package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:ZouChou
 * @Description:
 * @Date:Created in 14:06 2024/5/4
 * @Modified By:
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("购物车数据:{}",shoppingCartDTO);
        //判断当前购物车数据是否已存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果已存在，则增加数量
        if(list !=null && list.size()>0){
            ShoppingCart cat = list.get(0);
            cat.setNumber(cat.getNumber()+1);
            shoppingCartMapper.updateNumberById(cat);
        }else {
            //如果不存在，则添加到购物车，数量默认为1
            //本次添加的菜品或套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId !=null){
                //添加菜品购物车，数量默认为1
                DishDTO dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setMeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setMeal.getName());
                shoppingCart.setImage(setMeal.getImage());
                shoppingCart.setAmount(setMeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }




    }

    /**
     * 购物车列表
     * @return
     */
    public List<ShoppingCart> ShowShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */

    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);

    }

    /**
     * 删除购物车其中一个
     * @param shoppingCartDTO
     */
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询当前购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //判断当前购物车数据是否已存在
        if(list !=null && list.size()>0){
            ShoppingCart cat = list.get(0);
            Integer number = cat.getNumber();
            if(number ==1){
                shoppingCartMapper.deleteById(cat.getId());
            }else {
                cat.setNumber(number-1);
                shoppingCartMapper.updateNumberById(cat);
            }
        }
    }


}
